﻿using System;
using System.Web.UI.WebControls;
using Cvk.News;

namespace BatDongSan.Manager
{
    public partial class ManNewsGanLoai : System.Web.UI.Page
    {
        public static int _id;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _id = Convert.ToInt32(Request.QueryString["id"]);
                if (_id > 0)
                {
                    BindNewsType(_id);
                    BindEventType(_id);
                }
            }
        }
        protected void BindNewsType(int idNews)
        {
            try
            {
                ckblNewsType.DataSource = NewsBll.claNewsTypeDetail.GetList(1, Utility.strConnect);
                ckblNewsType.DataTextField = "TypeName";
                ckblNewsType.DataValueField = "NewsTypeId";
                ckblNewsType.DataBind();

                foreach (ListItem li in ckblNewsType.Items)
                    li.Selected = NewsBll.claNews_NewsType.Check(idNews, Convert.ToInt32(li.Value), Utility.strConnect);
            }
            catch { }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListItem li in ckblNewsType.Items)
                {
                    Cvk_News_NewsType info = new Cvk_News_NewsType();
                    info.NewsId = _id;
                    info.NewsTypeId = Convert.ToInt32(li.Value);
                    if (li.Selected)
                        NewsBll.claNews_NewsType.Insert(info, Utility.strConnect);
                    else
                        NewsBll.claNews_NewsType.Delete(info, Utility.strConnect);
                }
                BindNewsType(_id);
                lbStatus.Text = "Cập nhật thành công";
            }
            catch { }
        }
        protected void BindEventType(int idNews)
        {
            try
            {
                ckbEvent.DataSource = NewsBll.claEvents.GetList(1, 10, Utility.strConnect);
                ckbEvent.DataTextField = "Event";
                ckbEvent.DataValueField = "EventId";
                ckbEvent.DataBind();

                foreach (ListItem li in ckbEvent.Items)
                    li.Selected = NewsBll.claNews_Event.Check(idNews, Convert.ToInt32(li.Value), Utility.strConnect);
            }
            catch { }
        }

        protected void btnEvent_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListItem li in ckbEvent.Items)
                {
                    Cvk_News_Event info = new Cvk_News_Event();
                    info.NewsId = _id;
                    info.EventId = Convert.ToInt32(li.Value);
                    if (li.Selected)
                        NewsBll.claNews_Event.Insert(info, Utility.strConnect);
                    else
                        NewsBll.claNews_Event.Delete(info, Utility.strConnect);
                }
                BindEventType(_id);
                lbStatus.Text = "Cập nhật thành công";
            }
            catch { }
        }
    }
}
